package org.create.bean;

import org.create.utils.ReadProperties;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


public class Constants {
    //SQL
    public static final String SHOW_TABLE_SQL = "SHOW TABLE STATUS";
    public static final String SHOW_FULL_FIELDS_SQL = "SHOW FULL FIELDS FROM @";
    public static final String SHOW_TABLE_INDEX = "SHOW INDEX FROM @";

    //SQL类型映射表
    public static final String[] SQL_DATE_TIME_TYPES = new String[]{"datetime", "timestamp"};
    public static final String[] SQL_DATE_TYPES = new String[]{"date"};
    public static final String[] SQL_DECIMAL_TYPE = new String[]{"decimal", "double", "float"};
    public static final String[] SQL_STRING_TYPE = new String[]{"char", "varchar", "text", "mediumtext", "longtext"};
    public static final String[] SQL_INTEGER_TYPE = new String[]{"int", "tinyint"};
    public static final String[] SQL_LONG_TYPE = new String[]{"bigint"};

    //bean配置
    public static boolean PREFIX;
    public static boolean SUFFIX;

    //项目名
    public static String PROJECT_NAME;

    //query参数
    public static String QUERY_FUZZY = ReadProperties.getValue("suffix.bean.query.fuzzy");
    public static String QUERY_TIME_START = ReadProperties.getValue("suffix.bean.query.time.start");
    public static String QUERY_TIME_END = ReadProperties.getValue("suffix.bean.query.time.end");

    //路径
    public static String SUFFIX_PARAM = ReadProperties.getValue("bean.suffix.param");
    public static String PATH_JAVA = "/java/";
    public static String PATH_RESOURCES = "/resources/";
    public static String SAVE_TABLE_INFO_PATH;
    public static String LOCAL_RESOURCE_PATH = "./create-demo/src/main/resources/";
    public static String PROJECT_ROOT = ReadProperties.getValue("project.root");
    public static String PATH_BASE = ReadProperties.getValue("project.root") + "/" + ReadProperties.getValue("project.name") + "/src/main";
    public static String PATH_BASE_RESOURCES;
    public static String PATH_BASE_JAVA;
    public static String TEST_ROOT;
    public static String PACKAGE_MAPPERS = ReadProperties.getValue("package.mappers");
    public static String PACKAGE_MAPPERS_XML = ReadProperties.getValue("package.mappers.xml");
    public static String PACKAGE_EXCEPTION = ReadProperties.getValue("package.exception");
    public static String PACKAGE_SERVICE = ReadProperties.getValue("package.service");
    public static String PACKAGE_CONTROLLER = ReadProperties.getValue("package.controller");
    public static String PACKAGE_UTILS = ReadProperties.getValue("package.utils");
    public static String PACKAGE_ENUMS = ReadProperties.getValue("package.enums");
    public static String PACKAGE_QUERY = ReadProperties.getValue("package.query");
    public static String PACKAGE_VO = ReadProperties.getValue("package.vo");
    public static String PACKAGE_BASE = ReadProperties.getValue("package.base");
    public static String PACKAGE_PO = ReadProperties.getValue("package.po");
    public static String PATH_PO;
    public static String PATH_MAPPERS;
    public static String PATH_QUERY;
    public static String PATH_VO;
    public static String PATH_SERVICE;
    public static String PATH_CONTROLLER;
    public static String PATH_MAPPERS_XML;
    public static List<String> PACKAGE_PARAM;

    //后缀|编码
    public static String JAVA_SUFFIX = ".java";
    public static String PROPERTIES_SUFFIX = ".properties";
    public static String XML_SUFFIX = ".xml";
    public static String YML_SUFFIX = ".yml";
    public static String FILE_ENCODING = ReadProperties.getValue("file.encoding");

    //注解
    public static String PATTERN_TIMEZONE;
    public static String YMD;
    public static String YMD_HMS;
    public static String FIELD_IGNORE;


    static {
        String project_name = ReadProperties.getValue("project.name");
        if (project_name == null) {
            throw new RuntimeException("项目名不能为空");
        } else {
            PROJECT_NAME = project_name;
        }
        String prefix = ReadProperties.getValue("bean.prefix");
        String suffix = ReadProperties.getValue("bean.suffix");
        String pattern_timezone = ReadProperties.getValue("pattern.timezone");
        String date = ReadProperties.getValue("pattern.date");
        String time = ReadProperties.getValue("pattern.time");
        String field_ignore = ReadProperties.getValue("field.ignore");
        String package_param = ReadProperties.getValue("package.param");

        PREFIX = prefix == null || Boolean.parseBoolean(prefix);
        SUFFIX = suffix == null || Boolean.parseBoolean(suffix);
        /*
         *格式化
         */
        PATTERN_TIMEZONE = pattern_timezone == null ? "GMT+8" : pattern_timezone;
        YMD = date == null ? "yyyy-MM-dd" : date;
        YMD_HMS = time == null ? "yyyy-MM-dd HH:mm:ss" : time;
        FIELD_IGNORE = field_ignore == null ? "" : field_ignore;
        /*
         *父路径
         */
        if (PATH_BASE.lastIndexOf("/") + 1 == PATH_BASE.length()) {
            PATH_BASE_JAVA = PATH_BASE + PATH_JAVA.substring(1);
            PATH_BASE_RESOURCES = PATH_BASE + PATH_RESOURCES.substring(1);
        } else {
            PATH_BASE_JAVA = PATH_BASE + PATH_JAVA;
            PATH_BASE_RESOURCES = PATH_BASE + PATH_RESOURCES;
        }
        PATH_BASE_JAVA = PATH_BASE_JAVA + PACKAGE_BASE.replace(".", "/") + "/";
        if (PROJECT_ROOT.lastIndexOf("/") + 1 == PROJECT_ROOT.length()) {
            TEST_ROOT = PROJECT_ROOT + PROJECT_NAME;
        } else {
            TEST_ROOT = PROJECT_ROOT + "/" + PROJECT_NAME;
        }
        TEST_ROOT += "/src/test/java";
        /*
         * 自定义包
         */
        if (package_param != null) {
            PACKAGE_PARAM = Arrays.stream(package_param.split(","))
                    .map(e -> PATH_BASE_JAVA + e.replace(".", "/") + "/")
                    .collect(Collectors.toList());
        }
        /*
         *固定包
         */
        PATH_PO = PATH_BASE_JAVA + PACKAGE_PO.replace(".", "/") + "/";
        PATH_MAPPERS = PATH_BASE_JAVA + PACKAGE_MAPPERS.replace(".", "/") + "/";
        PATH_QUERY = PATH_BASE_JAVA + PACKAGE_QUERY.replace(".", "/") + "/";
        PATH_VO = PATH_BASE_JAVA + PACKAGE_VO.replace(".", "/") + "/";
        PATH_MAPPERS_XML = PATH_BASE_RESOURCES + PACKAGE_BASE.replace(".", "/") + "/" + PACKAGE_MAPPERS_XML.replace(".", "/") + "/";
        PATH_SERVICE = PATH_BASE_JAVA + PACKAGE_SERVICE.replace(".", "/") + "/";
        PATH_CONTROLLER = PATH_BASE_JAVA + PACKAGE_CONTROLLER.replace(".", "/") + "/";
    }
}
